app.controller('sys_userViewController', ['$scope', 'httpWrapService', '$state', '$stateParams','dialogs', 'AlertFactory',
                    function ($scope, httpWrapService,  $state, $stateParams, dialogs, AlertFactory) {
    $scope.entity = {};

	$scope.selectRoles = {			
	};
	$scope.originalRoles = [];
    
    $scope.id = $stateParams.id;
    $scope.type = $stateParams.type || 'add';
    
    
//    $scope.$watch('entity.useunitId',function(newVal, oldVal){
//    	if(newVal) {
//    		if(newVal !== oldVal){
//    			httpWrapService.post('system/user/roles.do',{
//    		    	useunitId : $scope.entity.useunitId
//    		    }).then(function(res2){
//    		    	var datas = res2.data.data;
//    		    	var roleMaps = {};
//    	    		for(var index in $scope.originalRoles) {
//    	    			var roleId = $scope.originalRoles[index].id;
//    	    			roleMaps[roleId] = roleId;
//    	    		}
//    	    		
//    	    		for(var index in datas) {
//    	    			var data = datas[index];
//    	    			if(roleMaps[data.id]) {
//    	    				data.checked = true;
//    	    				$scope.selectRoles[data.id] = data.id;
//    	    			}
//    	    		}
//    	    		$scope.roles = datas;
//    		    });
//    		}
//    	}
//    });
    

    if($scope.id) {
    	httpWrapService.post('system/user/view/' + $scope.id + '.do').then(function (res) {
    		$scope.entity = res.data.entity;
    		$scope.type = 'update';
    		$scope.confirmPassword = $scope.entity.password;
    		
    		$scope.roles = res.data.map.roles;
    	});
    }

	$scope.setting = {}


}]);